//
//  Tienda.m
//  ProyectoMoviTiendas
//
//  Created by Daniel Paez on 3/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Tienda.h"

@implementation Tienda

@synthesize direccion,nombre,telefono,horaAbrir,categorias,horaCerrar,descripcion,diasAbierto;

- (id)init
{
    self = [super init];
    if (self) {
        categorias = [[NSMutableArray alloc] init];
    }
    return self;
}

//Método que retorna una fecha en el formato "Día1 a Día2, de hora1 a hora2 "
-(NSString *) darHorario
{
    NSMutableString *diaIni = [NSMutableString stringWithString:@""];
    NSMutableString *diaFin = [NSMutableString stringWithString:@""];
    
    NSString* horaIni;
    NSString* horaCer;
    
    //if(diasAbierto==1)
    //{
  //      diaIni=@"Lunes";
    //    diaFin=@"Viernes";
    //}
    
    int theNumber=diasAbierto;
    
    
    NSInteger numberCopy = theNumber; 
    // Prepend "0" or "1", depending on the bit
  
    [diaIni insertString:((numberCopy & 1) ? @"Sáb, " : @"") atIndex:0];
    numberCopy >>= 1;
    [diaIni insertString:((numberCopy & 1) ? @"Vie, " : @"") atIndex:0];
    numberCopy >>= 1;
    [diaIni insertString:((numberCopy & 1) ? @"Jue, " : @"") atIndex:0];
    numberCopy >>= 1;
    [diaIni insertString:((numberCopy & 1) ? @"Mié, " : @"") atIndex:0];
    numberCopy >>= 1;
    [diaIni insertString:((numberCopy & 1) ? @"Mar, " : @"") atIndex:0];
    numberCopy >>= 1;
    [diaIni insertString:((numberCopy & 1) ? @"Lun, " : @"") atIndex:0];
    numberCopy >>= 1;
    [diaIni insertString:((numberCopy & 1) ? @"Dom, " : @"") atIndex:0];
    numberCopy >>= 1;
    
    if(1<=horaAbrir<=12)
    {
        horaIni = [NSString stringWithFormat:@"%d:00 am", horaAbrir];
        
    }
    else
    {
        horaIni = [NSString stringWithFormat:@"%d:00 am", horaAbrir-12];        
    }
    
    if(1<=horaCerrar<=12)
    {
        horaCer = [NSString stringWithFormat:@"%d:00 pm", horaAbrir];
        
    }
    else
    {
        horaCer = [NSString stringWithFormat:@"%d:00 pm", horaAbrir-12];        
    }
    
    return [NSString stringWithFormat:@"%@de %@ a %@.",  diaIni, horaIni,  horaCer];
   
    
    
}


- (void) encodeWithCoder:(NSCoder*) coder
{
    // direccion,nombre,telefono,horaAbrir,categorias,horaCerrar,descripcion,diasAbierto
    
    [coder encodeObject:[self direccion] forKey:@"direccion"];
    [coder encodeObject:[self nombre] forKey:@"nombre"];
    [coder encodeObject:[self telefono] forKey:@"telefono"];
    [coder encodeObject:[self categorias] forKey:@"categorias"];
    [coder encodeObject:[self descripcion] forKey:@"descripcion"];
    [coder encodeInt:[self horaAbrir] forKey:@"horaAbrir"];
    [coder encodeInt:[self horaCerrar] forKey:@"horaCerrar"];
    [coder encodeInt:[self diasAbierto] forKey:@"diasAbierto"];
    
    //    [coder encodeFloat:[self strokeWidth] forKey:@"stroke_width"];
}


- (id) initWithCoder:(NSCoder*) coder
{
    //[self init];
        self = [self init];
    self.direccion = [coder decodeObjectForKey:@"direccion"];
    self.nombre = [coder decodeObjectForKey:@"nombre"];
    self.telefono = [coder decodeObjectForKey:@"telefono"];
    self.categorias = [coder decodeObjectForKey:@"categorias"];  
    self.descripcion = [coder decodeObjectForKey:@"descripcion"];  
    self.horaAbrir = [coder decodeIntForKey:@"horaAbrir"];  
    self.horaCerrar = [coder decodeIntForKey:@"horaCerrar"];  
    self.diasAbierto = [coder decodeIntForKey:@"diasAbierto"];  
    //[self setStrokeWidth:[coder decodeFloatForKey:@"stroke_width"]];
    return self;
}
    
- (BOOL)isEqual:(id)other {
    if (other == self)
        return YES;
    if (!other || ![other isKindOfClass:[self class]])
        return NO;
    return [self isEqualToWidget:other];
}

- (BOOL)isEqualToWidget:(Tienda *)aWidget {
    if (self == aWidget)
        return YES;
    if (![self.nombre isEqualToString:aWidget.nombre])
        return NO;
    
    return YES;
}
@end
